package com.jastar.android.test.abstracts;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

public abstract class AbstractNavigateActivity extends AppCompatActivity implements View.OnClickListener {

    private final Map<Integer, Class<?>> jumpMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getActivityId());

        updateJumpMap(jumpMap);

        jumpMap.keySet().forEach(k -> super.findViewById(k).setOnClickListener(this));
    }

    @Override
    public void onClick(View v) {
        jumpMap.forEach((key, value) -> {
            if (key.equals(v.getId())) jumpActivity(value);
        });
    }

    private void jumpActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        super.startActivity(intent);
    }

    protected abstract int getActivityId();

    protected abstract void updateJumpMap(Map<Integer, Class<?>> map);

}